package com.mingtu.jgspatrol.activity;

import android.view.View;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.blankj.utilcode.util.KeyboardUtils;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.mingtu.bio.fragment.BioFragment;
import com.mingtu.bio.fragment.FindBioFragment;
import com.mingtu.bio.view.FilterAnimalView;
import com.mingtu.common.Eventbus.MyEventBus;
import com.mingtu.common.adapter.MyFragmentPagerAdapter;
import com.mingtu.common.entity.TabEntity;
import com.mingtu.common.utils.Constant;
import com.mingtu.jgspatrol.R;
import com.mingtu.jgspatrol.base.MyBaseActivity;
import com.mingtu.jgspatrol.databinding.ActivityBioFindBinding;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;

public class BioFindActivity extends MyBaseActivity<ActivityBioFindBinding> {

    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private final String[] mTitles = {"生物资源", "资源采集"};
    private BioFragment bioFragment;
    private FindBioFragment findBioFragment;
    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_bio_find;
    }

    @Override
    protected void initView() {
        super.initView();
        setModuleTitle("生物发现");
    }

    @Override
    protected void initData() {
        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i]));
        }

        bioFragment = BioFragment.getInstance();
        findBioFragment = FindBioFragment.getInstance();

        mFragments.add(bioFragment);
        mFragments.add(findBioFragment);


        binding.viewPager.setOffscreenPageLimit(2);
        binding.viewPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), mFragments));
        binding.viewPager.setCurrentItem(0);
        binding.viewPager.addOnPageChangeListener(new myOnPageChangeListener());
        binding.viewPager.setNoScroll(false);
        binding.commonTab.setTextBold(1);//选中的时候加错
        binding.commonTab.setTabData(mTabEntities);
        binding.commonTab.setOnTabSelectListener(new MyOnTabSelectListener());
        binding.filterAnimalView.setOnClickListener(new FilterAnimalView.OnClickListener() {
            @Override
            public void onClickCancel() {
                closeRightDrawers();
            }

            @Override
            public void onClickReset(String kmName, String classType, String levelName, String genusName) {
                if (bioFragment != null)
                    bioFragment.reset(kmName, genusName, levelName, classType); //重置数据
                closeRightDrawers();
            }

            @Override
            public void onClickSure(String kmName, String classType, String levelName, String genusName) {
                closeRightDrawers();
                if (bioFragment != null)
                    bioFragment.refresh(kmName, genusName, levelName, classType); //筛选之后 重置数据
            }
        });
        addClickListener(this, binding.ivFilte);
    }

    @Override
    protected void getData() {

    }

    public BioFragment getBioFragment() {
        return bioFragment;
    }

    public void setBioFragment(BioFragment bioFragment) {
        this.bioFragment = bioFragment;
    }

    public FindBioFragment getFindBioFragment() {
        return findBioFragment;
    }

    public void setFindBioFragment(FindBioFragment findBioFragment) {
        this.findBioFragment = findBioFragment;
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.iv_filte:
                boolean isOpen = binding.layoutDrawerAnimal.isDrawerOpen(binding.layoutRightDrawer);
                if (isOpen) {
                    closeRightDrawers();
                } else {
                    openRightDrawer();
                    KeyboardUtils.hideSoftInput(BioFindActivity.this);
                }
                break;
        }
    }

    /**
     * 关闭右侧边栏
     */
    public void closeRightDrawers() {
        binding.layoutDrawerAnimal.closeDrawer(binding.layoutRightDrawer);
    }

    /**
     * 打开右侧边栏
     */
    public void openRightDrawer() {
        binding.layoutDrawerAnimal.openDrawer(binding.layoutRightDrawer);
    }


    //点击tab 监听
    private class MyOnTabSelectListener implements OnTabSelectListener {

        @Override
        public void onTabSelect(int position) {
            binding.viewPager.setCurrentItem(position);
            if (position == 1) {
                //处于发现界面的时候，隐藏筛选
                binding.ivFilte.setVisibility(View.GONE);
                EventBus.getDefault().post(new MyEventBus(Constant.CLOSE_FILTE_BIO));
            } else if (position == 0) {
                binding.ivFilte.setVisibility(View.VISIBLE);
                EventBus.getDefault().post(new MyEventBus(Constant.SHOW_FILTE_BIO));
            }
        }

        @Override
        public void onTabReselect(int position) {

        }
    }

    private class myOnPageChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            binding.commonTab.setCurrentTab(position);
            if (position == 1) {
                //处于发现界面的时候，隐藏筛选
                binding.ivFilte.setVisibility(View.GONE);
                EventBus.getDefault().post(new MyEventBus(Constant.CLOSE_FILTE_BIO));
            } else if (position == 0) {
                binding.ivFilte.setVisibility(View.VISIBLE);
                EventBus.getDefault().post(new MyEventBus(Constant.SHOW_FILTE_BIO));
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }
}